<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>1202_生命周期(旧)</title>
</head>

<body>
  <div id="test"></div>
  <!-- react核心库 -->
  <script type="text/javascript" src="../js/react.development.js"></script>
  <!-- 用于支持react操作DOM -->
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <!-- 用于将jsx转为js -->
  <script type="text/javascript" src="../js/babel.min.js"></script>

  <script type="text/babel">

    /**
     * 创建组件
     *  生命周期函数：生命周期钩子函数
     */
    class Count extends React.Component {
      constructor(props) {
        console.log('Count === constructor');
        super(props)
        // 初始化状态
        this.state = {
          count: 1 // 计数
        }
      }


      /**
       * +1
       */
      add = () => {
        this.setState({count: this.state.count + 1})
      }

      /**
       * 卸载组件
       */
       remove = () => {
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      /**
       * 组件将要挂载
       */
      componentWillMount() {
        console.log('Count === componentWillMount');
      }

      /**
       * 组件挂载完毕
       */
      componentDidMount() {
        console.log('Count === componentDidMount');
      }

      /**
       * 组件卸载之前
       */
      componentWillUnmount() {
        console.log('Count === componentWillUnmount');
      }

      /**
       * 初始化渲染，状态更新重新渲染
       */
      render() {
        console.log('Count === render');
        const { count } = this.state
        return (
          <div>
            <h2>当前计数：{count}</h2><br />
            <button onClick={this.add}>点我+1</button>
          </div>
        )
      }
    }

    // 2.渲染虚拟DOM到页面
    ReactDOM.render(<Count />, document.getElementById('test'))

  </script>
</body>

</html>